package cn.itsource.blog.service.imp;



import cn.itsource.blog.domain.Article;
import cn.itsource.blog.domain.ArticleType;
import cn.itsource.blog.mapper.ArticleMapper;
import cn.itsource.blog.query.ArticleQuery;
import cn.itsource.blog.service.ArticleService;
import cn.itsource.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ArticleServiceImp implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> selectAll() {
        try {

            JsonResult.success("查询用户数据成功！");
            return articleMapper.selectAll();

        } catch (Exception e) {
            e.printStackTrace();
            JsonResult.error();
            return null;
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            articleMapper.delete(id);
            JsonResult.success("删除用户数据成功！");

        } catch (Exception e) {
            e.printStackTrace();
            JsonResult.error();
        }

    }

    @Override
    public void update(Article article) {
        if (article.id == null) {
            articleMapper.add(article);
            System.out.println("新增");
            JsonResult.success("新增用户数据成功！");
        } else {
            articleMapper.update(article);
            System.out.println("修改");
            JsonResult.success("修改用户数据成功！");
        }

    }

    @Override
    public void deleteMore(Long[] ids) {
        articleMapper.deleteMore(ids);
    }

    @Override
    public PageInfo<Article> queryArticle(ArticleQuery articleQuery) {
        // 设置分页参数
        PageHelper.startPage(articleQuery.getCurrentPage(), articleQuery.getPageSize());
        System.out.println(articleQuery);

        List<Article> articles = articleMapper.queryArticle(articleQuery);
        System.out.println(articles);
        return new PageInfo<>(articles);
    }
   /* @Override
    public List<ArticleType> tree(){
        //1.获取所有ArticleType
        List<Article> articleTypes = articleMapper.selectAll();
        HashMap<Integer, Article> typeHashMap = new HashMap<>();
        //2.将ArticleType添加到map集合中,id作为键,ArticleType作为值,方便查询
        for (Article article : articles) {
            typeHashMap.put(article.getId(),article);
        }
        //3.定义一个List<ArticleType>用与存放无限级树
        List<ArticleType> typeTree = new ArrayList<>();
        for (Article article : articles) {
            //4.获取父类型id,并且进行判断,如果parentId==null就说明是顶层,直接加到typeTree中
            Integer parentId = article.getParentId();
            if (parentId==null){
                //5.说明自己是顶级层,将自己添加到typeTree中
                typeTree.add(articleType);
            }else {
            *//*
                如果parentId!=null,说明有parentId
                  1.先根据parentId查询map集合中的父对象
                  2.然后判断,如果父对象!=null
                  3.获取父对象的children,将自己加入到children中
                  4.最后返回typeTree
             *//*
                ArticleType parentArticleType = typeHashMap.get(parentId);
                //防止父类型名称为空时,出现空指针异常
                if (parentArticleType !=null){
                    List<ArticleType> children = parentArticleType.getChildren();
                    children.add(articleType);
                }
            }
        }
        return typeTree;*/






}
